package addon.simplylock.theme.example;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;

/**    Simply Lockscreen Skin Developer Kit
 *
 *	Copyright (C) 2011  Jorge Garrido Oval, alias: FireZenk
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>
 **/

/**
 * DONT MODIFY THIS CODE *
 */
class ListOfImages {
    private Resources res;
    private final String THEME;
    private static final List<String> THEME_STRINGS = new ArrayList<String>();
    private static final List<Bitmap> THEME_IMAGES = new ArrayList<Bitmap>();
    /**
     * Check method getWorkingDir() is using Environment.getExternalStorageDirectory().toString()
     * to avoid problems with sdcard, storage and many others;
     */
    private static String PATH = "/Android/data/org.firezenk.simplylock/themes";
    private static String DIR;
    private static String FILE;

    public ListOfImages(Resources res) throws IOException {
        this.res = res;
        THEME = res.getString(R.string.theme_name);
        DIR = getWorkingDir();
        FILE = getBinFileName();

        THEME_IMAGES.add(0, decodeResource(R.drawable.b0));
        THEME_IMAGES.add(1, decodeResource(R.drawable.b1));
        THEME_IMAGES.add(2, decodeResource(R.drawable.b2));
        THEME_IMAGES.add(3, decodeResource(R.drawable.b3));
        THEME_IMAGES.add(4, decodeResource(R.drawable.b4));
        THEME_IMAGES.add(5, decodeResource(R.drawable.b5));
        THEME_IMAGES.add(6, decodeResource(R.drawable.b6));
        THEME_IMAGES.add(7, decodeResource(R.drawable.b7));
        THEME_IMAGES.add(8, decodeResource(R.drawable.b8));
        THEME_IMAGES.add(9, decodeResource(R.drawable.b9));
        THEME_IMAGES.add(10, decodeResource(R.drawable.lockscroll));
        THEME_IMAGES.add(11, decodeResource(R.drawable.lockthumb));
        THEME_IMAGES.add(12, decodeResource(R.drawable.mail));
        THEME_IMAGES.add(13, decodeResource(R.drawable.next));
        THEME_IMAGES.add(14, decodeResource(R.drawable.pause));
        THEME_IMAGES.add(15, decodeResource(R.drawable.phone));
        THEME_IMAGES.add(16, decodeResource(R.drawable.play));
        THEME_IMAGES.add(17, decodeResource(R.drawable.prev));
        THEME_IMAGES.add(18, decodeResource(R.drawable.sms));
        THEME_IMAGES.add(19, decodeResource(R.drawable.statusbar));
        THEME_IMAGES.add(20, decodeResource(R.drawable.volumescroll));
        THEME_IMAGES.add(21, decodeResource(R.drawable.volumethumb));

        THEME_STRINGS.add(0, decodeString(res.getResourceName(R.drawable.b0)));
        THEME_STRINGS.add(1, decodeString(res.getResourceName(R.drawable.b1)));
        THEME_STRINGS.add(2, decodeString(res.getResourceName(R.drawable.b2)));
        THEME_STRINGS.add(3, decodeString(res.getResourceName(R.drawable.b3)));
        THEME_STRINGS.add(4, decodeString(res.getResourceName(R.drawable.b4)));
        THEME_STRINGS.add(5, decodeString(res.getResourceName(R.drawable.b5)));
        THEME_STRINGS.add(6, decodeString(res.getResourceName(R.drawable.b6)));
        THEME_STRINGS.add(7, decodeString(res.getResourceName(R.drawable.b7)));
        THEME_STRINGS.add(8, decodeString(res.getResourceName(R.drawable.b8)));
        THEME_STRINGS.add(9, decodeString(res.getResourceName(R.drawable.b9)));
        THEME_STRINGS.add(10, decodeString(res.getResourceName(R.drawable.lockscroll)));
        THEME_STRINGS.add(11, decodeString(res.getResourceName(R.drawable.lockthumb)));
        THEME_STRINGS.add(12, decodeString(res.getResourceName(R.drawable.mail)));
        THEME_STRINGS.add(13, decodeString(res.getResourceName(R.drawable.next)));
        THEME_STRINGS.add(14, decodeString(res.getResourceName(R.drawable.pause)));
        THEME_STRINGS.add(15, decodeString(res.getResourceName(R.drawable.phone)));
        THEME_STRINGS.add(16, decodeString(res.getResourceName(R.drawable.play)));
        THEME_STRINGS.add(17, decodeString(res.getResourceName(R.drawable.prev)));
        THEME_STRINGS.add(18, decodeString(res.getResourceName(R.drawable.sms)));
        THEME_STRINGS.add(19, decodeString(res.getResourceName(R.drawable.statusbar)));
        THEME_STRINGS.add(20, decodeString(res.getResourceName(R.drawable.volumescroll)));
        THEME_STRINGS.add(21, decodeString(res.getResourceName(R.drawable.volumethumb)));

        imagesToSD();
    }

    /**
     * Maybe has no sense because on UNIX Systems the file separator usually used is slash '/'
     *
     * @return return current system file separator is the is any otherwise returns slash '/'
     */
    private String getFileSeparator() {
        return System.getProperty("file.separator", "/");
    }

    private String decodeString(String s) {
        return s.substring(s.indexOf(getFileSeparator()), s.length());
    }

    private void createDirectory(String dir) {
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    private void imagesToSD() throws IOException {
        String dd = DIR + getFileSeparator() + THEME;
        createDirectory(dd);

        //copy necessary font from assests to theme directory
        copyFont();

        File bin = new File(FILE);
        FileOutputStream fos = new FileOutputStream(bin);

        ObjectOutputStream out;
        out = new ObjectOutputStream(fos);
        out.writeObject(THEME_STRINGS);
        out.close();

        for (int i = 0; i < THEME_IMAGES.size(); i++) {
            File bbb = new File(dd + getFileSeparator() + THEME_STRINGS.get(i));
            FileOutputStream fff;
            fff = new FileOutputStream(bbb);
            THEME_IMAGES.get(i).compress(CompressFormat.PNG, 100, fff);
            fff.flush();
            fff.close();

            /**
             Free up the memory associated with this bitmap's pixels, and mark the bitmap as "dead",
             meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing.
             @link http://developer.android.com/reference/android/graphics/Bitmap.html#recycle()
             */
            THEME_IMAGES.get(i).recycle();
        }
    }

    private android.graphics.Bitmap decodeResource(int drawableId) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 1;
        return BitmapFactory.decodeResource(res, drawableId, opts);
    }

    private String getWorkingDir(){
        return Environment.getExternalStorageDirectory().toString() + PATH;
    }

    private String getBinFileName(){
          return FILE = DIR + "/" + THEME + "/" + THEME + ".bin";
    }
    private void copyFont() throws IOException {
        String dd = DIR + "/" + THEME + "/font";
        File bin = new File(dd);
        FileOutputStream fos = null;
        fos = new FileOutputStream(bin);
        ObjectOutputStream out = null;
        out = new ObjectOutputStream(fos);
        out.writeObject(new File("fonts/font.ttf"));
        out.close();
    }

}
